/**
 * FileUploadController.java
 * $Author: chris $
 *
 */
package uk.ac.manchester.tewpl.iceiface.server.service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.explorium.api.exception.KnowledgeBaseException;
import org.explorium.api.repository.kb.KBRepositoryProvider;
import org.explorium.api.repository.kb.KBRepositoryProviderManager;
import org.explorium.api.repository.kb.KnowledgeBase;
import org.explorium.core.bus.Bus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Roland
 * @version $Id: FileUploadController.java,v 1.3.2.2 2008/08/17 18:28:08 roland
 *          Exp $
 * 
 */
@Controller
@RequestMapping("/upload/*")
public class FileUploadServiceImpl {
	private static final Log LOG = LogFactory
			.getLog(FileUploadServiceImpl.class);
	@Autowired
	private Bus bus;
	private KBRepositoryProvider provider;

	public void setBus(Bus bus) {
		this.bus = bus;

		// Set KnowledgeBase Repository Provider
		this.provider = bus.getExtension(KBRepositoryProviderManager.class)
				.getKBRepositoryProvider("owl");
		if (this.provider == null) {
			LOG.debug("Provider is NULL :-(");
		}
	}

	@RequestMapping("kb")
	public void kb(HttpServletResponse response, @RequestParam String uri,
			@RequestParam String workspace,
			@RequestParam MultipartFile file) throws IOException,
			KnowledgeBaseException {
		LOG.debug("Uploading a file");
		KnowledgeBase kb = provider.getKnowledgeBase(uri);
		if (kb != null) {
			if (workspace != null && !workspace.equals("")) 
				kb.setActiveWorkspace(workspace);
			try {
				kb.startTransaction();
				kb.loadFromStream(file.getInputStream());
				kb.commitTransaction();
			} catch (Exception e) {
				kb.rollbackTransaction();
				throw new KnowledgeBaseException(e);
			}
		}
		response.getWriter().println("Knowledge Base Uploaded Successfully");
		response.getWriter().flush();
	}

	@RequestMapping("kb-download")
	public void downloadKB(HttpServletResponse response,
			@RequestParam String uri,
			@RequestParam String wsuri) throws IOException,
			KnowledgeBaseException {
		LOG.debug("Downloading a file");
		KnowledgeBase kb = provider.getKnowledgeBase(uri);
		if (kb != null) {
			response.setContentType("application/rdf+xml");
			SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
			response.setHeader("Content-Disposition", "attachment; filename="
					+ kb.getInfo().getName().replace(" ", "") + "-" + df.format(new Date()) + ".owl");
			try {
				kb.setActiveWorkspace(wsuri);
				kb.dumpOWLToStream(response.getOutputStream());
			} catch (Exception e) {
				throw new KnowledgeBaseException(e);
			}
		}
		response.getOutputStream().flush();
	}
	
	@RequestMapping("excel")
	public void uploadEXCEL(HttpServletResponse response, @RequestParam String uri,
			@RequestParam String workspace,
			@RequestParam MultipartFile file){
		/*try {
			TableImport.excelImport(provider, uri, workspace, file.getInputStream());
		} catch (KnowledgeBaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
}
